# -*- mode: python; -*-

import sys

Import(
    [
        "env",
        "use_libunwind",
        "version_extra",
        "version_parts",
    ]
)

env = env.Clone()

env.InjectMongoIncludePaths()

env.AppendUnique(
    FORCEINCLUDES=[
        "mongo/platform/basic.h",
    ],
)

env.SConscript(
    must_exist=1,
    dirs=[
        "base",
        "bson",
        "client",
        "crypto",
        "db",
        "dbtests",
        "embedded",
        "executor",
        "idl",
        "installer",
        "logv2",
        "platform",
        "resmoke",
        "rpc",
        "s",
        "scripting",
        "shell",
        "stdx",
        "tools",
        "transport",
        "unittest",
        "util",
        "watchdog",
    ],
    exports=[
        "env",
    ],
)

sys.path.append("src/mongo")

# NOTE: The 'base' library does not really belong here. Its presence
# here is temporary. Do not add to this library, do not remove from
# it, and do not declare other libraries in this file.
baseEnv = env.Clone()

if use_libunwind:
    baseEnv.InjectThirdParty("unwind")
baseEnv.InjectThirdParty("intel_decimal128")

# Stage the top-level mongodb banners

distsrc = env.Dir("#distsrc")
env.AutoInstall(
    target="$PREFIX",
    source=[
        distsrc.File("README"),
        # TODO: we need figure out what to do when we use a different
        # THIRD-PARTY-NOTICES for example, with Embedded
        distsrc.File("THIRD-PARTY-NOTICES"),
        distsrc.File("MPL-2"),
    ],
    AIB_COMPONENT="common",
    AIB_COMPONENTS_EXTRA=["dist", "dist-test"],
    AIB_ROLE="base",
)

# If no module has introduced a file named LICENSE-Enterprise.txt then this
# is a Community build, so inject the AGPL and the Community license
enterprise_license = [
    banner for banner in env["MODULE_BANNERS"] if banner.name == "LICENSE-Enterprise.txt"
]
if not enterprise_license:
    env.Append(MODULE_BANNERS=[distsrc.File("LICENSE-Community.txt")])

# All module banners get staged to the top level of the tarfile, so we
# need to fail if we are going to have a name collision.
module_banner_filenames = set([f.name for f in env["MODULE_BANNERS"]])
if not len(module_banner_filenames) == len(env["MODULE_BANNERS"]):
    # TODO: Be nice and identify conflicts in error.
    env.FatalError("ERROR: Filename conflicts exist in module banners.")

env.AutoInstall(
    target="$PREFIX",
    source=env.get("MODULE_BANNERS", []),
    AIB_COMPONENT="common",
    AIB_COMPONENTS_EXTRA=["dist", "dist-test"],
    AIB_ROLE="base",
)

if env.TargetOSIs("darwin", "macOS"):
    env.AutoInstall(
        target="$PREFIX",
        source=[
            env.File("#/etc/macos_mongodb.plist"),
        ],
        AIB_COMPONENT="common",
        AIB_COMPONENTS_EXTRA=["dist", "dist-test"],
        AIB_ROLE="base",
    )
